package main;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.util.Calendar;
import java.util.Enumeration;

import javax.swing.JOptionPane;

import common.AntiCrack;

public class Activation {

	private String mycode ;
	private final File f = new File("xycf.!mv");
	public Activation(){
		mycode = getCODE();
	}

	//
	// public static void main(String args[]) {
	// try {
	// Activation act = new Activation();
	// act.coding();
	// System.exit(0);
	// System.out.println(act.deCard("2NZ691XF901?407107791709419F4"));
	// System.exit(0);
	// System.out.println(act.stop());
	// } catch (Exception e) {
	// IllegalArgumentException illegalargumentexception = new
	// IllegalArgumentException(
	// e.toString());
	// AntiCrack.Fake(illegalargumentexception, e);
	// throw illegalargumentexception;
	// }
	// }

	private static String bytes2(byte[] bytes) {

		StringBuffer macString = new StringBuffer();
		byte currentByte;
		for (int i = 0; i < bytes.length; i++) {
			currentByte = (byte) ((bytes[i] & 240) >> 4);
			macString.append(Integer.toHexString(currentByte));
			currentByte = (byte) (bytes[i] & 5);
			macString.append(Integer.toHexString(currentByte));
			macString.append("-");
		}
		macString.delete(macString.length() - 1, macString.length());

		return macString.toString().toUpperCase();
	}
	
	public String getCODE() {
		try {
			Enumeration intefaces = NetworkInterface.getNetworkInterfaces();
			while (intefaces.hasMoreElements()) {
				NetworkInterface face = (NetworkInterface) intefaces
						.nextElement();
				if (face.getName().length() >= 3) {
					if ((face.getName()).substring(0, 3).equals("eth")
							|| (face.getName()).substring(0, 3).equals("net")) {

						byte[] mac = face.getHardwareAddress();
						if (mac != null && mac.length == 6) {
							return bytes2(mac);
						}
					}
				}
			}
		} catch (SocketException e) {
			e.printStackTrace();
		}
		return "-1";
	}

	public String Coding() {
		try {
			StringBuffer str = new StringBuffer();
			str.append(Interfere());
			for (int no = 0; no < 6; no++) {
				str.append(mycode.charAt(no * 3));
				str.append(Interfere());
			}
			return str.toString();
		} catch (Exception e) {
			IllegalArgumentException illegalargumentexception = new IllegalArgumentException(
					e.toString());
			AntiCrack.Fake(illegalargumentexception, e);
			throw illegalargumentexception;
		}
	}

	private String Interfere() {
		try {
			StringBuffer str = new StringBuffer();
			int no = (int) (Math.random() * 3 + 1);
			str.append(no);
			char ch;
			while (no > 0) {
				no--;
				ch = (char) ((int) (Math.random() * 26 + 65));
				str.append(ch);
			}
			return str.toString();
		} catch (Exception e) {
			IllegalArgumentException illegalargumentexception = new IllegalArgumentException(
					e.toString());
			AntiCrack.Fake(illegalargumentexception, e);
			throw illegalargumentexception;
		}
	}

	public String getB() {
		try {
			FileInputStream fis = null;
			InputStreamReader isr = null;
			BufferedReader br = null;
			String b;
			if (f.exists()) {
				try {
					fis = new FileInputStream(f);
					isr = new InputStreamReader(fis);
					br = new BufferedReader(isr);
					b = br.readLine();
					b = br.readLine();
					br.close();
					isr.close();
					fis.close();
					if (b != null && b != "") {
						if (null != deCard(b)) {
							return b;
						}
					}
				} catch (Exception e) {
					JOptionPane.showMessageDialog(null, "权限不足，请以管理员方式重新运行");
					System.exit(0);
				} finally {
					try {
						br.close();
					} catch (Exception e1) {
					}
					try {
						isr.close();
					} catch (Exception e1) {
					}
					try {
						fis.close();
					} catch (Exception e1) {
					}
				}
			}
			JOptionPane.showMessageDialog(null, "激活卡错误");
			System.exit(0);
			return null;

		} catch (Exception e) {
			IllegalArgumentException illegalargumentexception = new IllegalArgumentException(
					e.toString());
			AntiCrack.Fake(illegalargumentexception, e);
			throw illegalargumentexception;
		}
	}

	public String stop() {
		try {
			FileInputStream fis = null;
			InputStreamReader isr = null;
			BufferedReader br = null;
			String a;
			String b;
			String ab;
			if (f.exists()) {
				try {
					fis = new FileInputStream(f);
					isr = new InputStreamReader(fis);
					br = new BufferedReader(isr);
					a = br.readLine();
					b = br.readLine();
					br.close();
					isr.close();
					fis.close();
					if (decode(a)) {
						if (b != null && b != "") {
							if ((ab = deCard(b))!=null) {
								return ab;
							}
						} else {
							JOptionPane.showMessageDialog(null, "未激活");
						}
						return a;
					}

				} catch (Exception e) {
					JOptionPane.showMessageDialog(null, "权限不足，请以管理员方式重新运行");
					System.exit(0);
				} finally {
					try {
						br.close();
					} catch (Exception e1) {
					}
					try {
						isr.close();
					} catch (Exception e1) {
					}
					try {
						fis.close();
					} catch (Exception e1) {
					}
				}
			}
			JOptionPane.showMessageDialog(null, "请激活！");
			return writeCode();

		} catch (Exception e) {
			IllegalArgumentException illegalargumentexception = new IllegalArgumentException(
					e.toString());
			AntiCrack.Fake(illegalargumentexception, e);
			throw illegalargumentexception;
		}
	}

	private String writeCode() {
		try {
			String code = null;
			FileOutputStream fsw = null;
			OutputStreamWriter osw = null;
			BufferedWriter bw = null;
			try {

				fsw = new FileOutputStream(f);
				osw = new OutputStreamWriter(fsw);
				bw = new BufferedWriter(osw);
				code = Coding();
				bw.write(code);
			} catch (IOException e) {
				JOptionPane.showMessageDialog(null, "权限不足，请以管理员方式重新运行");
			} finally {
				try {
					bw.flush();
					bw.close();
					osw.close();
					fsw.close();
				} catch (Exception e) {

				}
			}
			return code;
		} catch (Exception e) {
			IllegalArgumentException illegalargumentexception = new IllegalArgumentException(
					e.toString());
			AntiCrack.Fake(illegalargumentexception, e);
			throw illegalargumentexception;
		}
	}

	private boolean decode(String a) {
		try {
			int no = 0;
			int no1 = 0;
			String b;
			b = "" + mycode.charAt(0) + mycode.charAt(3) + mycode.charAt(6) + mycode.charAt(9)
					+ mycode.charAt(12) + mycode.charAt(15);
			String mac = "";
			try {
				while (no < a.length()) {
					no1 = a.charAt(no) - 47;
					no = no + no1;
					if (mac.length() == 6)
						break;
					mac = mac + a.charAt(no);
					no++;
				}
				if(mac.length()<6)
					return false;
			} catch (Exception e) {
				return false;
			}
			if (b.equals(mac)) {
				return true;
			}
			return false;
		} catch (Exception e) {
			IllegalArgumentException illegalargumentexception = new IllegalArgumentException(
					e.toString());
			AntiCrack.Fake(illegalargumentexception, e);
			throw illegalargumentexception;
		}
	}

	String deCard(String cardno) {
		try {
			String relt = "";
			String time = "";
			int no = 0;
			int no1 = 0;
			char tip = (char) 1;
			try {
				while (no < cardno.length()) {
					no1 = cardno.charAt(no) - 47;
					no = no + no1;
					relt = relt + cardno.charAt(no);
					if (relt.length() == 6)
						break;
					no++;
					if (tip == (char) 1) {
					} else if (tip == cardno.charAt(no)) {
						no++;
					} else {
						break;
					}
					time = time + cardno.charAt(no);
					tip = cardno.charAt(no);
					no++;
				}
				if (relt.length() < 6)
					throw new Exception("er");
			} catch (Exception e) {
				JOptionPane.showMessageDialog(null, "错误的激活码！");
				return null;
			}
			// System.out.println(relt);
			// System.out.println(time);
			long end = Long.parseLong(time) << 24;
			Calendar can = Calendar.getInstance();
			Calendar now = Calendar.getInstance();
			now.setTimeInMillis(coding());
			can.setTimeInMillis(end);
			if (now.compareTo(can) > 0) {
				JOptionPane.showMessageDialog(null, "激活卡已过期！");
				return null;
			}
			if (can.getTimeInMillis() - now.getTimeInMillis() > 8640000L * 200) {
				JOptionPane.showMessageDialog(null, "错误的激活码！");
				return null;
			}
			return can.get(Calendar.YEAR) + "年" + can.get(Calendar.MONTH) + "月"
					+ can.get(Calendar.DAY_OF_MONTH) + "日"
					+ can.get(Calendar.HOUR_OF_DAY) + "时";
		} catch (Exception e) {
			IllegalArgumentException illegalargumentexception = new IllegalArgumentException(
					e.toString());
			AntiCrack.Fake(illegalargumentexception, e);
			throw illegalargumentexception;
		}
	}

	private Long coding() {
		try {
			String sTmp = "http://open.baidu.com/special/time/";
			try {
				URL url = new URL(sTmp);
				HttpURLConnection connect = (HttpURLConnection) url
						.openConnection();
				connect.setDoInput(true);
				BufferedReader in = new BufferedReader(new InputStreamReader(
						connect.getInputStream()));
				String line = null;
				long time = 0;
				while ((line = in.readLine()) != null) {
					if (line.indexOf("window.baidu_time(") != -1) {
						time = Long.parseLong(line.replaceAll("[^0-9]", ""));
						break;
					}
				}
				if (time == 0) {
					JOptionPane.showMessageDialog(null,
							"服务器错误！请重试，若仍然出现此消息，\n请联系QQ：281363415");
					System.exit(0);
				}
				return time;
			} catch (Exception e) {
				JOptionPane.showMessageDialog(null,
						"请确认您已连接网络\n并且本程序可以连接到internet");
				System.exit(0);
			}
			return 0L;
		} catch (Exception e) {
			IllegalArgumentException illegalargumentexception = new IllegalArgumentException(
					e.toString());
			AntiCrack.Fake(illegalargumentexception, e);
			throw illegalargumentexception;
		}
	}

	private String interfere() {

		int no1 = (int) (Math.random() * 2 + 1);
		System.out.println(no1);
		StringBuffer str = new StringBuffer();
		str.append(no1);
		while (no1 > 0) {
			no1--;
			char no = (char) (48 + Math.random() * 43);
			str.append(no);
		}
		return str.toString();
	}
}